<template>
  <view class="container">
    <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList" field="xinghao,beizhu,name">
      <view v-if="error">{{error.message}}</view>
      <view v-else-if="data">
		  <view style="display: flex;width: 90%;margin: 10rpx auto;">
		  	<view style="display: flex;flex: 1;">
		  		名字
		  	</view>
		  	<view style="display: flex;flex: 1;">
		  		型号
		  	</view>
		  	<view style="display: flex;flex: 2;">
		  		备注
		  	</view>
		  </view>
        <uni-list >
          <uni-list-item style="box-shadow: 1px 1px 5px #c2c2c2;width: 98%;margin: 0 auto;" v-for="(item, index) in data" :key="index" :clickable="true" @click="handleItemClick(item._id)">
            <template v-slot:body>
				<view style="display: flex;width: 100%;">
					<view style="display: flex;flex: 1;">
					  {{item.name}}
					</view>
					<view style="display: flex;flex: 1;">
					  {{item.xinghao}}
					</view>
					<view style="display: flex;flex: 2;color: darkgray;font-size: 24rpx;">
					  {{item.beizhu}}
					</view>
				</view>
              
            </template>
          </uni-list-item>
        </uni-list>
      </view>
      <uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
    </unicloud-db>
    <uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" />
  </view>
</template>

<script>
  const db = uniCloud.database()
  export default {
    data() {
      return {
        collectionList: "lipinka_xinghao",
        loadMore: {
          contentdown: '',
          contentrefresh: '',
          contentnomore: ''
        }
      }
    },
    onPullDownRefresh() {
      this.$refs.udb.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom() {
      this.$refs.udb.loadMore()
    },
    methods: {
      handleItemClick(id) {
        uni.navigateTo({
          url: './detail?id=' + id
        })
      },
      fabClick() {
        // 打开新增页面
        uni.navigateTo({
          url: './add',
          events: {
            // 监听新增数据成功后, 刷新当前页面数据
            refreshData: () => {
              this.$refs.udb.loadData({
                clear: true
              })
            }
          }
        })
      }
    }
  }
</script>

<style>
</style>
